/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.odichy.andfml.content;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.odichy.andfml.domain.FMLList;
import org.odichy.andfml.sax.FMLListHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

/**
 * AndFML, an FMyLife.com client on Google Android platform.
 *
 * @author Odin Liu<odinushuaia@gmail.com>
 *
 * @version 1.0.0
 */
public class FMLListFactory {
	public static final String LAST = "last";
	public static final String TOP = "top";
	public static final String TOP_DAY = "top_day";
	public static final String TOP_MONTH = "top_month";
	public static final String TOP_WEEK = "top_week";
	public static final String FLOP = "flop";
	public static final String FLOP_DAY = "flop_day";
	public static final String FLOP_WEEK = "flop_week";
	public static final String FLOP_MONTH = "flop_month";
	public static final String CAT_LOVE = "love";
	public static final String CAT_MONEY = "money";
	public static final String CAT_KIDS = "kids";
	public static final String CAT_WORK = "work";
	public static final String CAT_HEALTH = "health";
	public static final String CAT_INTIMACY = "intimacy";
	public static final String CAT_MISCELLANEOUS = "miscellaneous";
	
	private String url;
	private FMLList fmlList;
	private String message;
	
	public FMLListFactory(String baseURL, String type, int page, String key, String language) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURL);
		sb.append("/");
		sb.append(type);
		sb.append("/");
		sb.append(page);
		sb.append("?key=");
		sb.append(key);
		sb.append("&language=");
		sb.append(language);
		this.url = sb.toString();
		//Log.i("----FML----", url);
		buildFMLList();
	}
	
	public FMLListFactory(String baseURL, String type, int page, String key, String language, String token) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURL);
		sb.append("/");
		sb.append(type);
		sb.append("/");
		sb.append(page);
		sb.append("?key=");
		sb.append(key);
		sb.append("&language=");
		sb.append(language);
		sb.append("&token=");
		sb.append(token);
		this.url = sb.toString();
		//Log.i("----FML----", url);
		buildFMLList();
	}
	
	private void buildFMLList() {
		try {
			URL ourl = new URL(this.url);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlReader = parser.getXMLReader();
			FMLListHandler handler = new FMLListHandler();
			xmlReader.setContentHandler(handler);
			
			InputSource is = new InputSource(ourl.openStream());
			xmlReader.parse(is);
			this.fmlList = handler.getFmlList();
		}
		catch(Exception e) {
			this.fmlList = null;
			message = e.toString();
		}
	}
	
	public String getErrorMessage() {
		return this.message;
	}

	public FMLList getFmlList() {
		return fmlList;
	}
}
